package com.xul.yygh.msm.service.Impl;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.xul.yygh.msm.service.MsmService;
import model.com.xul.yygh.vo.msm.MsmVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author xul
 * @create 2021-07-14 20:46
 */
@Service
public class MsmServiceImpl implements MsmService {

    /**
     * 发送短信验证码
     *
     * @param phone
     * @param code
     * @return
     */
    @Override
    public boolean send(String phone, String code) {
        // 判断手机号是否为空
        if (StringUtils.isEmpty(phone)) {
            return false;
        }

        // 整合容联云短信发送验证码
        //生产环境请求地址：app.cloopen.com
        String serverIp = "app.cloopen.com";
        //请求端口
        String serverPort = "8883";
        //请使用管理控制台中已创建应用的APPID
        //String appId = "appId";
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(serverIp, serverPort);
        sdk.setAccount("8a216da87a332d53017aa4ca903927bf", "3fc87a3927df4b1796c9fce7c39e2bf6");
        sdk.setAppId("8a216da87a332d53017aa4ca913c27c5");

        sdk.setBodyType(BodyType.Type_JSON);
        //绑定的测试手机号
        String to = phone;
        //短信模板
        String templateId = "1";
        //分钟参数
        String[] datas = {code, "5"};
        HashMap<String, Object> result = sdk.sendTemplateSMS(to, templateId, datas);
        if ("000000".equals(result.get("statusCode"))) {
            //正常返回输出data包体信息（map）
            HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for (String key : keySet) {
                Object object = data.get(key);
                System.out.println(key + " = " + object);
            }
        } else {
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
        }
        return true;

    }

    /**
     * mq发送短信
     *
     * @param msmVo
     * @return
     */
    @Override
    public boolean send(MsmVo msmVo) {
        if (!StringUtils.isEmpty(msmVo.getPhone())) {
            String code = (String) msmVo.getParam().get("code");
            boolean isSend = this.send(msmVo.getPhone(), code);
            return isSend;
        }
        return false;
    }


}
